<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      :title="article.title"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-if="loading" class="article-loading" />
    <!-- /加载中 loading -->
      <div v-if="is404" class="error">
        <p>文章让狗叼走了！</p>
        <van-button @click="$router.back()">后退</van-button>
        <van-button @click="$router.push('/')">返回主页</van-button>
      </div>
      <div v-else>
           <!-- <div v-else class="detail">...</div> -->
          <!-- 文章详情 -->
          <div class="detail">
            <h3 class="title">{{ article.title }}</h3>
            <!-- 使用组件 -->
            <author :author="article">
              <!-- 调用接口 -->
              <!-- v-show="article.aut_id.toString() !== $store.state.userInfo.id" -->
              <van-button
                round
                size="small"
                type="info"
                v-show="!isYourSelf"
                @click="hSwitchFollow"
              >{{ article.is_followed?'取消关注':'+ 关注'}}</van-button>
            </author>

            <div class="content">
              <div v-html="article.content"></div>
            </div>
            <van-divider>END</van-divider>
            <div class="zan">
              <van-button round size="small" hairline type="primary" plain icon="good-job-o">点赞</van-button>
              &nbsp;&nbsp;&nbsp;&nbsp;
              <van-button round size="small" hairline type="danger" plain icon="delete">不喜欢</van-button>
            </div>
          </div>
          <!-- /文章详情 -->
          <!-- 文章的评论 -->
          <article-comment :article_id = "$route.params.id"></article-comment>
      </div>
  </div>
</template>

<script>
import ArticleComment from './comment'
import Author from '@/components/author'
import { unFollowUser, followUser } from '@/api/user.js'
import { getArticle } from '@/api/article.js'

export default {
  name: 'ArticleIndex',
  data () {
    return {
      is404: false, // 文章是否存在
      loading: true, // 控制加载中的 loading 状态
      article: { } // 当前的文章
    }
  },
  computed: {
    isYourSelf () {
      if (!this.article.aut_id) {
        return false
      }
      // eslint-disable-next-line eqeqeq
      return this.article.aut_id == this.$store.state.userInfo.id
    }
  },
  created () {
    this.loadArticle()
  },
  components: {
    Author,
    ArticleComment
  },
  beforeRouteLeave (to, from, next) {
    console.log('你现在在文章详情页，你将要离开这个页面')
    console.log(to, from)
    if (to.path === '/login' && this.$store.state.tokenInfo.token) {
      next({ path: '/' })
    } else {
      next()
    }
  },
  methods: {
    async loadArticle () {
      try {
        // 文章编号
        const id = this.$route.params.id
        const result = await getArticle(id)
        this.article = result.data.data
        // 获取数据成功之后就停止
        this.loading = false
      } catch (err) {
        // 取消加载状态
        this.loading = false
        if (err && err.response && err.response.status === 404) {
          // 确定文章没有找到
          this.is404 = true
        } else {
          this.$toast.fail('文章加载失败')
        }
      }
    },
    // 如果这篇文章是自己写的，则不能自己关注自己！
    async hSwitchFollow () {
      try {
        const isFollowed = this.article.is_followed
        const autId = this.article.aut_id
        // 根据当前的 is_followed 的值决定调用哪个方法
        // 如果 is_followed 是true;再点击按钮，表示要做取消关注
        if (isFollowed) {
          await unFollowUser(autId)
        } else {
          await followUser(autId)
        }
        // 更新视图
        this.article.is_followed = !isFollowed

        this.$toast.success('操作成功')
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
